#include<iostream>
#include<unordered_map>
#include<fstream>
#include"Logger.hpp"

class Dictionary
{
private:
    void LoadConf()
    {
        std::ifstream in(_path);
        if(!in.is_open())
        {
            LOG(LogLevel::ERROR) << "file open error";
            return;
        }   
        std::string line;
        while(std::getline(in, line))
        {
            LOG(LogLevel::DEBUG) << "get dict message:" << line;
            size_t pos = line.find(": ");
            if(pos == std::string::npos)
            {
                LOG(LogLevel::WARNING) << "dict message error:" << line;
                continue;
            }
            //dog: 狗
            std::string word = line.substr(0, pos);
            std::string val = line.substr(pos + 2);
            _dict.insert({word, val});
        }
    }
public:
    Dictionary(const std::string& path)
        :_path(path)
    {
        LoadConf();
    }

    std::string Translation(const std::string& word, const std::string& ip, uint16_t port)
    {
        auto ret = _dict.find(word);
        if(ret == _dict.end())
            return "unknown";
        return ret->first + ":" + ret->second;
    }

private:
    std::string _path;
    std::unordered_map<std::string, std::string> _dict;
};